package com.magic233.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.magic233.entity.Message;
import com.magic233.entity.Users;
import com.magic233.mapper.MessageMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ClassName: MessageServiceImpl
 * Description:
 * date: 2021/6/5 13:14
 * author: zouyuan
 */
@Service
public class MessageServiceImpl implements MessageService {

    @Autowired
    private MessageMapper messageMapper;

    private final SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");

    @Override
    public int sendMessage(Long senderid, Long recipientid, String content) {
        Long id = new Date().getTime();
        String time = simpleDateFormat.format(id);
        Message message = new Message();
        message.setId(id);
        message.setSenderid(senderid);
        message.setRecipientid(recipientid);
        message.setContent(content);
        message.setStatus("未读");
        message.setTime(time);

        int re = messageMapper.insert(message);
        return re;
    }


    @Override
    public Map<Object, Object> selectMessage(Message message, Integer pageNum, Integer pageSize) {
        System.out.println(pageNum);
        System.out.println(pageSize);
        if (pageNum != null && pageSize != null) {
            System.out.println("设置分页");
            PageHelper.startPage(pageNum, pageSize);
        }

        Page<Message> messages = messageMapper.selectMessage(message);
        long total = messages.getTotal();
        Map<Object, Object> map = new HashMap<>();
        map.put("messages", messages);
        map.put("total", total);
        return map;
    }

    @Override
    public int updateMessageStatus(Users user, Long id) {
        Message message = new Message();
        message.setId(id);
        message.setRecipientid(user.getId());
        message.setStatus("已读");
        int re = messageMapper.updateStatus(message);
        return re;
    }
}
